import { rpcGetRequest, rpcRequest } from './base';
import _ from 'lodash';
import { request, gatewayRequest } from '@ydc/workbench-tools';

export function listStencilSets(data: any) {
  const param = {
    request: {
      stencilTypeEnum: data,
    },
  };
  return rpcRequest('bpmn/stencil-sets/bpmn.json', param);
}

export function getModel(data: any) {
  const param = {
    request: data,
  };
  return rpcRequest('bpmn/models/front/model/get.json', param);
}

export function saveModel(data: any) {
  const param = {
    request: data,
  };
  return rpcRequest('bpmn/models/front/model/editor/saveAndDeploy.json', param);
}

export function deleteModel(data: any) {
  const param = {
    request: data,
  };
  return rpcRequest('bpmn/models/front/model/deleteForDeploy.json', param);
}

export function getDeptTree(data: any) {
  const param = {
    request: data,
  };
  return rpcRequest('usercenter/department/queryDeptTree.json', param);
}

export const parseTreeData = (d: any) => {
  const td: any = {
    title: d.name,
    label: d.name,
    value: d.id,
    key: d.id,
  };

  if (d.children && d.children.length > 0) {
    td.children = d.children.map(parseTreeData);
  }
  return td;
};

export function searchMultiIndexFulltext(data: any) {
  const param = {
    request: data,
  };
  return rpcRequest('search/searchMultiIndexFulltext.json', param);
}

export function flowRule() {
  const params = {
    request: {},
  };
  return rpcRequest('bpmn/stencil-sets/bpmnGatewayRule.json', params);
}

export function queryByIdentityIds(data: { identityIds: any }) {
  const params = {
    identityQueryReq: data,
  };
  return rpcRequest('usercenter/identity/queryByIdentityIds.json', params);
}

export function getAppBuildPages(app: any) {
  const params = {
    applicationRequest: {
      appId: app.id,
      version: app.version,
    },
  };

  return rpcRequest('metadata/getAppPages.json', params).then((result) =>
    result.data.map((page: any) =>
      _.pick(page, ['title', 'pagePath', 'pagePreset', 'gmtCreate', 'gmtModified', 'permCode']),
    ),
  );
}

export function searchRoles(data: any) {
  const params = {
    queryRoleRequest: data,
  };
  return rpcRequest('rbac/role/queryRole.json', params);
}

export function buildApprovalFlowModel(data: any) {
  const params = data;
  return rpcRequest('biz/hr/recruitment/buildApprovalFlowModel.json', params);
}

export function buildApprovalFlowModelHr(data: any) {
  const params = data;
  return rpcRequest('biz/hr/core_hr/bpmn/buildApprovalFlowModel.json', params);
}

export function getServiceArrangeList(data: any) {
  return rpcGetRequest('graph/queryAllGraphVersion.json', data);
}

export function createdServiceArrange(data: any) {
  return request('/gateway/graph/saveAndDeploy.json', {
    method: 'post',
    data,
  });
}

export function deleteServiceArrange(data: any) {
  return rpcGetRequest('graph/deleteById.json', data);
}

export function modalList(data: any) {
  const params = {
    request: data,
  };
  return rpcRequest('bpmn/models/front/list.json', params);
}

export function createdWorkFlow(data: any) {
  const params = {
    request: data,
  };
  return rpcRequest('bpmn/models/front/create.json', params);
}

export function getPositionPageList() {
  const param = {
    req: {
      pageNo: 1,
      pageSize: 200,
    },
  };
  return rpcRequest('usercenter/positionService/getPositionPageList.json', param);
}

export function getCompanyPageList() {
  const param = {
    req: {
      pageNo: 1,
      pageSize: 200,
    },
  };
  return rpcRequest('usercenter/companyService/getCompanyPageList.json', param);
}

export function getAllLevel() {
  const param = {};
  return rpcRequest('usercenter/levelService/getEnableLevel.json', param);
}

export function getAddressPageList() {
  const param = {
    req: {
      pageNo: 1,
      pageSize: 200,
    },
  };
  return rpcRequest('usercenter/addressService/getAddressPageList.json', param);
}

export function deploy(data: any) {
  const param = {
    request: data,
  };
  return rpcRequest('bpmn/repository/deployments/front-model/deploy.json', param);
}

export function getFlowVariablesByIdentityId(app: any, data: any) {
  const param = {
    ...app,
    ...data,
  };
  return rpcRequest('biz/hr/core_hr/bpmn/getFlowVariablesByIdentityId.json', param);
}

export function getTaskDetailForProcessShow(params: any) {
  const param = {
    request: {
      name: 'taskDetailForProcessShow',
      ...params,
    },
  };
  return gatewayRequest('bpmn/query/historic-task-instances/process-instance/taskDetailProcessShow.json', { param });
}

export function taskDetailProcessShow(id: number) {
  const param = {
    request: {
      name: 'taskDetailForProcessShow',
      processInstanceId: id,
    },
  };
  return rpcRequest('bpmn/query/historic-task-instances/process-instance/taskDetailProcessShow.json', param);
}
